from optparse import OptionParser

#parse des options separees par des ","
def read_list_arg(option, opt, value, parser):
  print value
  lst = [m.split(':') for m in value.split(',')]
  print lst
  setattr(parser.values, option.dest, lst)

def read_list_arg1(option, opt, value, parser):
  setattr(parser.values, option.dest, value.split(','))

#parse des options separees par des ":"
def read_list_arg2(option, opt, value, parser):
  setattr(parser.values, option.dest, value.split(':'))

def opt_parser_dimeco_base() :
  parser = OptionParser()
  parser.add_option("-o", "--fileout", dest="fileout", default = "out.js",
                    help="write report to the json file FILEOUT [default -o out.js]", metavar="FILEOUT")
  parser.add_option("-q", "--quiet", action="store_false", dest="verbose", default=True,
                    help="don't print status messages to stdout" )
  return parser


def opt_parser_dimeco_index():
  parser = opt_parser_dimeco_base()
  parser.add_option("-d", "--directory", dest="directory", default = "./Corpus/",
                    help="use the corpus at the directory DIRECTORY [default -d ./Corpus/]", metavar="DIRECTORY")
  parser.add_option("-i", "--index", dest="index", default = "docs.txt",
                    help="read the index INDEX in the directory DIRECTORY [default -i docs.txt]", metavar="INDEX")
  parser.add_option("-t", "--target_directory", dest="target_directory", default = "./Out/",
                    help="move the corpus at the directory TGTDIRECTORY [default -t ./Out/]", metavar="TGTDIRECTORY")

  return parser

def opt_parser_dimeco():
  parser = opt_parser_dimeco_base()
  parser.add_option("-d", "--directory", dest="directory", default = "./Corpus/",
                    help="use the corpus at the directory DIRECTORY [default -d ./Corpus/]", metavar="DIRECTORY")
  parser.add_option("-i", "--index", dest="index", default = "docs.txt",
                    help="read the index INDEX in the directory DIRECTORY [default -i docs.txt]", metavar="INDEX")
  parser.add_option("-c", "--segmenter", dest="segmenter", default = ["l","1"],
                    type = "string", action = "callback", callback = read_list_arg2,
                    help="use de segmenter SEG [default : -c l:1] Values : {c:[0-9], l:[0-9], a}",
                    metavar="SEG")
  parser.add_option("-s", "--segmentDistance", dest="segmentDistance", default = "lv",
                    help='use de distance between segments SEGDIST [default : -s lv] ' +
                         'Values : {lv|levenshtein, ie|innerEntropy, j|jaro, jw|jaro_winkler, eq|equals}',
                    metavar="SEGDIST")
  parser.add_option(
    "-l", "--documentDistanceFilter", dest="documentDistanceFilter", default = ["c","t","h","c","t"],
    type = "string", action = "callback", callback = read_list_arg1,
    help="DOCDISTFILTER applied on the segment matrix [default : -l c,t,h,c,t] (-l f1,f2,f1 will apply f1 then f2 and f1) "+
         "Values : {h|hungarian, t|threshold, c|convolute, jv|lapjv}", metavar="DOCDISTFILTER")

  return parser




#  parser.add_option("-t", "--filter", dest= "documentFilter", default = ["t"],
#                    type = "string", action = "callback", callback = read_list_arg1,
#                    help="FILTER applied on each document [default : -t t] (-t f1,f2,f1 will apply f1 then f2 and f1) "+
#                    "Values : {t, s}", metavar="FILTER")




#  parser.add_option(
#    "-d", "--documentDistance", dest="documentDistance", default = "sum",
#    help="compute the distance DOCDIST on the segment matrix [default : -d sum]" +
#         "Values : {sum}", metavar="DOCDIST")
